package com.tnl.app.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.tnl.app.entity.Interview;
import com.tnl.base.BaseDAO;

@Repository
public class InterViewDAO {
	private static final String SQL_GET_ID = "select * from tb_interview  where id = ?";
	
	private static final String SQL_GET_RECOMMEND = "select * from tb_interview  where  recommendId= ?";
	private static final String SQL_GET_RECOMMEND_WITH_LIMIT = "select * from tb_interview  where  recommendId= ? ";
	private static final String SQL_GET_RECOMMEND_COUNT = "select COUNT(*) from tb_interview  where  recommendId= ?";
	private static final String SQL_GET_OBJECT_RECOMMEND="select * from tb_interview where recommendId=?";
	
	private static final String SQL_GET_ALL = "select * from tb_interview ";
	private static final String SQL_INSERT = " insert into tb_interview (recommendId, itype, updateTime,status,createby,createTime,interviewDate,interviewSite,interviewer,interviewContact,round,remark)"
			+ " values(?,?,?,?,?,?,?,?,?,?,?,?)";
	private static final String SQL_UPDATE ="update tb_interview set recommendId=?, itype=?, updateTime=?,status=?,interviewDate=?,interviewSite=?,interviewer=? where id =?";
	
	private static final String SQL_UPDATE_STATUS = "UPDATE tb_interview SET status=? WHERE id=?";
	
	private static final String SQL_UPDATE_TYPE_STATUS = "UPDATE tb_interview SET itype=?,status=? WHERE id=?";
	
	private static final String SQL_GET_STATUS_COUNT = "select COUNT(*) from tb_interview  where  recommendId= ? and status=?";
	
	public static enum Status {
		wait, start, processing, completed, rejected,accepted,cancelled
	}
	
	public List<Interview> getALL() {
		return BaseDAO.queryForList(Interview.class, SQL_GET_ALL);
	}

	public Interview getById(int Id) {
		return BaseDAO.queryForObject(Interview.class, SQL_GET_ID, Id);
	}
	
	public Long save(Interview interview) {
		return BaseDAO.save( SQL_INSERT, interview.getRecommendId(), interview.getItype(), 
				interview.getUpdateTime(),interview.getStatus(),interview.getCreateBy(),interview.getCreateTime(),
				interview.getInterviewDate(),interview.getInterviewSite(),interview.getInterviewer(),interview.getInterviewContact(),interview.getRound(),interview.getRemark());
	}
	
	public long update(Interview interview) {
		return BaseDAO.execute( SQL_UPDATE, interview.getRecommendId(), interview.getItype(), 
				interview.getUpdateTime(),interview.getStatus(),interview.getInterviewDate(),interview.getInterviewSite(),interview.getInterviewer(),interview.getId());
	}

	public List<Interview> getByKey(int recommendId) {
		return BaseDAO.queryForList(Interview.class, SQL_GET_RECOMMEND, recommendId);
	}

	public Interview getByRecommendId(int recommendId){
		return BaseDAO.queryForObject(Interview.class, SQL_GET_OBJECT_RECOMMEND, recommendId);
	}
	public int updateStatus(int status, int interviewid) {
		return BaseDAO.execute(SQL_UPDATE_STATUS, status, interviewid);
	}
	
	//public Long getCountOfAll(int recommendid) {
	//	return (Long) BaseDAO.queryOneRowOneCol(SQL_GET_RECOMMEND_COUNT, recommendid);
	//}

	public Long getCountOfAll(Integer recommendid, Integer status) {
		String sql = SQL_GET_RECOMMEND_COUNT;

		if (null != status) {
			sql += " and status =" + status + " ";
		}

		return (Long) BaseDAO.queryOneRowOneCol(sql, recommendid);
	}

	public List<Interview> getAllWithLimit(Integer recommendid, Integer status,
			long start, long length) {
		String sql = SQL_GET_RECOMMEND_WITH_LIMIT;

		if (null != status) {
			sql += " and status =" + status;
		}
		sql += " LIMIT ?, ?";
		return BaseDAO.queryForList(Interview.class, sql, recommendid, start,
				length);
	}
	
	public int updateType(int type,int status,int interviewid){
		return BaseDAO.execute(SQL_UPDATE_TYPE_STATUS, type,status, interviewid);
	}

}
